SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE Proc [dbo].[SP_TASALD_FLEF_I01]  
/*---------------------------------------------------------------------*/  
/*----- Empresa          : OFISIS S.A.                            -----*/  
/*----- Cliente          : OFISIS S.A.                            -----*/  
/*----- Sistema          : Contabilidad                           -----*/  
/*----- Modulo           : Empresa                                -----*/  
/*----- Programa         : Mayorizacion Flujo de Efectivo         -----*/  
/*----- Nombre           : SP_TASALD_FLEF_I01                   -----*/  
/*----- Script           : TASALD_FLEF                            -----*/  
/*----- Desarrollado     : Israel Estrada Polanco                 -----*/  
/*----- Fecha            : 23/06/1997                             -----*/  
/*----- Base de Datos    : MS SQL Server                          -----*/  
/*----- Version BD       : 6.5                                    -----*/  
/*----- Invoca a SP      :                                        -----*/  
/*---------------------------------------------------------------------*/  
/*---------------------------------------------------------------------*/  
/*----- Modificado 1 por : percy garcia                           -----*/  
/*----- Fecha Modifica   : 05/06/2004                             -----*/  
/*----- Detalle Modifica : usuario creacion y modif               -----*/  
/*---------------------------------------------------------------------*/  
/*---------------------------------------------------------------------*/  
/*----- Modificado 2 por : percy garcia                           -----*/  
/*----- Fecha Modifica   : 12/07/2010                             -----*/  
/*----- Detalle Modifica : se ordeno select del cursor CU_TASALD_FLEF_I01_1-----*/  
/*---------------------------------------------------------------------*/  
/*---------------------------------------------------------------------*/  
/*----- Drop Proc SP_TASALD_FLEF_I01                              -----*/  
/*----- Grant Execute On SP_TASALD_FLEF_I01 To PUBLIC             -----*/  
/*----- SP_TASALD_FLEF_I01                                     -----*/  
/*---------------------------------------------------------------------*/  
@ISCO_EMPR       TD_VC_002,  
@ISCO_USUA          TD_VC_008,
@IDFE_USUA          TD_DT_001,
@INNU_CNTB_EMPR       TD_IN_001_CONT,  
@INNU_ANO1       TD_IN_004_ANNO,  
@INNU_MES1       TD_IN_002_MESE,  
@INNU_ANO2       TD_IN_004_ANNO,  
@INNU_MES2       TD_IN_002_MESE  
As  
Declare   
 @VSCN_CNTB_EMPR       TD_VC_020_CNTA,  
 @VSCN_CNTA_ESPC_CARG  TD_VC_020_CNTA,  
 @VSCN_CNTA_ESPC_ABON  TD_VC_020_CNTA,  
 @VSCO_OPRC            TD_VC_003,  
 @VSNS_ASTO        TD_VC_010,  
 @VSTI_OPER_CNTB       TD_VC_003,   
 @VSCO_UNID_RGST_CNTB  TD_VC_003,  
 @VNNU_MES         TD_IN_002_MESE,  
 @VNNU_MES3        TD_IN_002_MESE,  
 @VNIM_MVTO_CNTB       TD_NU_016_004,  
 @VSCO_USUA            TD_VC_008_USUA ,  
 @VDFE_SIST            TD_DT_001_SIST  
Begin transaction SP_TASALD_FLEF_I01  
Delete From TASALD_FLEF Where  CO_EMPR = @ISCO_EMPR   
 And    NU_CNTB_EMPR = @INNU_CNTB_EMPR   
 And    NU_ANNO  = @INNU_ANO1  
 And    NU_MESE Between @INNU_MES1 And @INNU_MES2   
If @@Error <> 0     /*si falla el Delete*/  
   Begin   
     Rollback Transaction  
     Return  
   End  
Select @VNNU_MES3 = @INNU_MES1  
While (@VNNU_MES3 <= @INNU_MES2)  
      Begin  
        Insert TASALD_FLEF   
        Select @ISCO_EMPR, @INNU_CNTB_EMPR, @INNU_ANO1,    
               @VNNU_MES3, CO_CNTA_FLEF, 0, 0,  
               replicate('0',@VNNU_MES3) + '1' + replicate('0',20-(@VNNU_MES3+1)),  
               @ISCO_USUA, @IDFE_USUA ,@ISCO_USUA , @IDFE_USUA  
        From  TMPLAN_FLEF   
        where CO_EMPR = @ISCO_EMPR  
If @@Error <> 0     /*si falla el Insert */  
   Begin   
     Rollback Transaction  
     Return  
   End  
Select @VNNU_MES3 = @VNNU_MES3 + 1  
End  
Declare CU_TASALD_FLEF_I01_1 Cursor  
For  
 Select --Distinct 
  CO_UNID_CNTB, CO_OPRC_CNTB, NU_ASTO, NU_MESE  
  From TXMVTO_CNTB   
   Where CO_EMPR = @ISCO_EMPR      And  
   NU_CNTB_EMPR  = @INNU_CNTB_EMPR And  
   NU_ANNO        = @INNU_ANO1     And  
   NU_MESE Between @INNU_MES1 And @INNU_MES2 And  
   --SubString(CO_CNTA_EMPR,1,2) = '10'   And   
   CO_CNTA_EMPR Like  '10%'   And   
   SI_MVTO_CNTB = 'APR'  
   Group By CO_UNID_CNTB, CO_OPRC_CNTB, NU_ASTO, NU_MESE
   Order By NU_MESE  
   
 Open CU_TASALD_FLEF_I01_1  
 Fetch CU_TASALD_FLEF_I01_1 Into @VSCO_UNID_RGST_CNTB, @VSCO_OPRC,  
     @VSNS_ASTO, @VNNU_MES  
While @@fetch_status = 0  
 Begin  
  Declare CU_TASALD_FLEF_I01_2 Cursor  
   For  
    Select CO_CNTA_EMPR, TI_OPER, IM_MVTO_CNTB  
    From TXMVTO_CNTB   
    Where CO_EMPR = @ISCO_EMPR           And  
          NU_CNTB_EMPR  = @INNU_CNTB_EMPR      And          
          CO_UNID_CNTB  = @VSCO_UNID_RGST_CNTB And  
          CO_OPRC_CNTB  = @VSCO_OPRC           And  
          NU_ANNO       = @INNU_ANO1           And  
          NU_MESE       = @VNNU_MES            And  
          NU_ASTO       = @VSNS_ASTO           And   
          SI_MVTO_CNTB = 'APR'  
    Open CU_TASALD_FLEF_I01_2  
    Fetch CU_TASALD_FLEF_I01_2 Into @VSCN_CNTB_EMPR, @VSTI_OPER_CNTB,@VNIM_MVTO_CNTB  
 While @@fetch_status = 0  
  Begin  
     Select @VSCN_CNTA_ESPC_CARG = CO_FLEF_CARG,  
     @VSCN_CNTA_ESPC_ABON = CO_FLEF_ABON  
     From  TMPLAN_EMPR  
     Where CO_EMPR = @ISCO_EMPR  And CO_CNTA_EMPR = @VSCN_CNTB_EMPR  
     And   NU_ANNO = 9999  
  
   
     If @VSCN_CNTA_ESPC_CARG IS NOT NULL And @VSCN_CNTA_ESPC_ABON IS NOT NULL  
       Begin  
          If @VSTI_OPER_CNTB = 'ABO'  
           Begin  
            Update TASALD_FLEF  
            Set IM_TOTA_CARG = IM_TOTA_CARG + Round(@VNIM_MVTO_CNTB,2) ,
           CO_USUA_MODI = @ISCO_USUA,
           FE_USUA_MODI = @IDFE_USUA  
            Where  CO_EMPR      = @ISCO_EMPR      And  
                   NU_CNTB_EMPR = @INNU_CNTB_EMPR And  
                   NU_ANNO      = @INNU_ANO1      And  
                   NU_MESE      = @VNNU_MES       And  
                   CO_CNTA_FLEF = @VSCN_CNTA_ESPC_CARG  
   
            If @@Error <> 0  
             Begin   
               Close CU_TASALD_FLEF_I01_2  
               Deallocate CU_TASALD_FLEF_I01_2  
               Close CU_TASALD_FLEF_I01_1  
               Deallocate CU_TASALD_FLEF_I01_1  
               Rollback Transaction  
               Return  
             End  
           End  
         Else  
           If @VSTI_OPER_CNTB = 'CAR'             Begin  
              Update TASALD_FLEF  
              Set IM_TOTA_ABON  = IM_TOTA_ABON + Round(@VNIM_MVTO_CNTB,2)   ,
           CO_USUA_MODI = @ISCO_USUA,
           FE_USUA_MODI = @IDFE_USUA 
              Where CO_EMPR     = @ISCO_EMPR      And  
                    NU_CNTB_EMPR      = @INNU_CNTB_EMPR And  
                    NU_ANNO           = @INNU_ANO1      And  
                    NU_MESE           = @VNNU_MES       And  
                    CO_CNTA_FLEF      = @VSCN_CNTA_ESPC_ABON   
   
              If @@Error <> 0  
               Begin   
                 Close CU_TASALD_FLEF_I01_2   
                 Deallocate CU_TASALD_FLEF_I01_2  
                 Close CU_TASALD_FLEF_I01_1  
                 Deallocate CU_TASALD_FLEF_I01_1  
                 Rollback Transaction  
                 Return  
               End  
            End  
         End   
   
       Fetch CU_TASALD_FLEF_I01_2 Into @VSCN_CNTB_EMPR, @VSTI_OPER_CNTB, @VNIM_MVTO_CNTB  
    End /* CU_TASALD_CNTB_ESPC_EMPR_I1_2 */  
    Close CU_TASALD_FLEF_I01_2  
    Deallocate CU_TASALD_FLEF_I01_2  
  Fetch CU_TASALD_FLEF_I01_1 Into @VSCO_UNID_RGST_CNTB, @VSCO_OPRC, @VSNS_ASTO, @VNNU_MES  
 End /* CU_TASALD_CNTB_ESPC_EMPR_I1_1 */  
   
 Close CU_TASALD_FLEF_I01_1  
 Deallocate CU_TASALD_FLEF_I01_1  
 Commit tran  
 Return  
/*------------------------------------- Fin -----------------------------------------*/  







GO
